package com.lvmama.pangolin.timer.processor;




import java.util.HashMap;
import java.util.List;
import java.util.Map;















import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.JSONUtil;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.lmm.LMMCareProduct;
import com.lvmama.pangolin.lmm.service.LMMCareProductService;
import com.lvmama.pangolin.service.TimerService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.HttpSignUtil;
import com.lvmama.pangolin.util.HttpsUtil;
import com.lvmama.pangolin.util.HttpsUtil.HttpResponseWrapper;
import com.lvmama.pangolin.util.SpringContextUtil;


public class PushFullCareProduct extends CommonJob {

	@Autowired
	private LMMCareProductService careProductService;
	
	@Autowired
	private TimerService timerService;
	
	private String url = "http://221.228.99.246:8246/pangolin/";
	
	private Logger logger = Logger.getLogger(PushFullCareProduct.class);
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		logger.info("同步最新数据开始状态开始...");
		try
		{
			if(careProductService == null)
			{
				careProductService = (LMMCareProductService) SpringContextUtil.getBean(LMMCareProductService.class);
			}
			int count = 0;
			int limit = 100;
			int time = 0;
			String productId = null;
			String minProductId = null;
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("limit", limit);
			List<LMMCareProduct> list = null;
			url = ConfigManager.getInstance().getParamValue(parameter, "URL",url);
			while(true)
			{
				param.put("productId", productId);
				list = careProductService.queryProductbyProd(param);
				if(list != null && !list.isEmpty())
				{
					if(time == 0)
					{
						minProductId = list.get(0).getProductId();
					}
					count = list.size();
					productId = list.get(count -1).getProductId();
					for (LMMCareProduct lmmCareProduct : list)
					{
						lmmCareProduct.setUpdateTime(null);
						lmmCareProduct.setInsertTime(null);
					}
					
					String data = JSONArray.fromObject(list).toString();
					logger.info("request data : " + data);
					HttpResponseWrapper rs = HttpsUtil.requestPostData(url+"syn/pushFullProd", data, "application/json", "UTF-8");
					String str = rs.getResponseString();
					logger.info("respone str : " + str);
					if(StringTools.isValid(str))
					{
						JSONObject jo = JSONUtil.getObject(str);
						String code = jo.getString("code");
						if("0".equals(code))
						{
							logger.info("同步产品成功！ count :" + count);
						}
					}
				}
				
				if(count < limit)
				{
					Map<String,Object> getParam = new HashMap<String,Object>();
					getParam.put("minProduct", minProductId);
					getParam.put("maxProduct", productId);
					String sign = HttpSignUtil.getSignFromMap( getParam);
					HttpsUtil.requestGet(url + "syn/deleteProd?" + sign);
					break;
				}
				time ++;
			}
			
			
			
		}catch (Exception e)
		{
			logger.error("更新任务状态失败!",e);
		}
		logger.info("同步最新数据开始状态结束...");
	}
	

}
